package net.siufung.boot.executor;

import net.siufung.boot.executor.utils.ThreadMdcUtil;
import org.jetbrains.annotations.NotNull;
import org.slf4j.MDC;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

/**
 * @author 陈建峰
 * @date 2022/9/26 4:34 下午
 * @since V1.0.0
 */
public class MdcThreadPoolExecutor extends ThreadPoolTaskExecutor {

    private static final long serialVersionUID = 3361247536047762198L;

    public MdcThreadPoolExecutor() {
        super();
    }


    @Override
    public void execute(@NotNull Runnable command) {
        super.execute(ThreadMdcUtil.wrap(command, MDC.getCopyOfContextMap()));
    }

    @NotNull
    @Override
    public Future<?> submit(@NotNull Runnable task) {
        return super.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

    @NotNull
    @Override
    public <T> Future<T> submit(@NotNull Callable<T> task) {
        return super.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
    }

}
